package com.tinkerapps.protocols.ssdp;

/**
 * Class for building SSDP requests
 */
public final class SSDPRequest {

	/**
	 * Constants
	 */
	
	private final static String NEW_LINE = "\r\n";
	private final static String HOST_HEADER = "HOST: ";
	private final static String MAN_HEADER = "MAN: ";
	private final static String MX_HEADER = "MX: ";
	private final static String ST_HEADER = "ST: ";
	private final static String MAN_VALUE_DISCOVER = "\"ssdp:discover\"";
	private final static String M_SEARCH = "M-SEARCH * HTTP/1.1";
	
	/**
	 * Build search request
	 * @param what Specifies what to discover (value of 'ST' header in the request)
	 * @param maxReplyTime Maximum time (in seconds) for reply.
	 * @return
	 */
	public static String buildSearchRequest(String what, int maxReplyTime) {
		if (maxReplyTime <= 0)
			throw new IllegalArgumentException("argument 'maxReplyTime' is invalid");
		
		StringBuilder request = new StringBuilder();
		
		request.append(M_SEARCH).append(NEW_LINE);
		request.append(HOST_HEADER).append(Constants.IPv4).append(':').append(Integer.toString(Constants.PORT)).append(NEW_LINE);
		request.append(MAN_HEADER).append(MAN_VALUE_DISCOVER).append(NEW_LINE);
		request.append(MX_HEADER).append(Integer.toString(maxReplyTime)).append(NEW_LINE);
		request.append(ST_HEADER).append(what).append(NEW_LINE);

		return request.append(NEW_LINE).toString();
	}
}
